package com.youlai.boot.gym.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.gym.mapper.ClassSchedulesMapper;
import com.youlai.boot.gym.service.ClassSchedulesService;
import com.youlai.boot.gym.model.entity.ClassSchedules;
import com.youlai.boot.gym.model.form.ClassSchedulesForm;
import com.youlai.boot.gym.model.query.ClassSchedulesQuery;
import com.youlai.boot.gym.model.vo.ClassSchedulesVO;
import com.youlai.boot.gym.converter.ClassSchedulesConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 课程安排服务实现类
 *
 * @author hxc
 * @since 2025-06-01 15:47
 */
@Service
@RequiredArgsConstructor
public class ClassSchedulesServiceImpl extends ServiceImpl<ClassSchedulesMapper, ClassSchedules> implements ClassSchedulesService {

    private final ClassSchedulesConverter classSchedulesConverter;

    /**
    * 获取课程安排分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<ClassSchedulesVO>} 课程安排分页列表
    */
    @Override
    public IPage<ClassSchedulesVO> getClassSchedulesPage(ClassSchedulesQuery queryParams) {
        Page<ClassSchedulesVO> pageVO = this.baseMapper.getClassSchedulesPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取课程安排表单数据
     *
     * @param id 课程安排ID
     * @return 课程安排表单数据
     */
    @Override
    public ClassSchedulesForm getClassSchedulesFormData(Long id) {
        ClassSchedules entity = this.getById(id);
        return classSchedulesConverter.toForm(entity);
    }
    
    /**
     * 新增课程安排
     *
     * @param formData 课程安排表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveClassSchedules(ClassSchedulesForm formData) {
        ClassSchedules entity = classSchedulesConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新课程安排
     *
     * @param id   课程安排ID
     * @param formData 课程安排表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateClassSchedules(Long id,ClassSchedulesForm formData) {
        ClassSchedules entity = classSchedulesConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除课程安排
     *
     * @param ids 课程安排ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteClassScheduless(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的课程安排数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
